/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-08     Administrator       the first version
 */

/*bsp的初始化*/
#include <bsp.h>

/* 线程控制块 */
static rt_thread_t led_thread = RT_NULL;
static rt_thread_t key_thread = RT_NULL;

void bsp_init(void)
{
    led_init();
    keys_init();
    // 初始化按键驱动，传入事件处理函数
//    key_driver_init(key_event_handler_example);
}

/* LED线程入口函数 */
static void led_thread_entry(void *parameter)
{


    while (1)
    {
        led_control(LED_R_PIN, 1);
        rt_thread_mdelay(500);  // 延时500ms
        led_control(LED_R_PIN, 0);
        rt_thread_mdelay(500);
    }
}

/* key线程入口函数 */
static void key_thread_entry(void *parameter)
{


    while (1)
    {
        keys_scan();
        for (int i = 0; i < 3; i++) {
            enum key_event event = get_key_event(i);
            if (event != KEY_EVENT_NONE) {
                rt_kprintf("Key%d: ", i+1);

                switch (event) {
                case KEY_EVENT_CLICK:
                    rt_kprintf("Click\n");
                    if (i == 0 ) {
                        led_control(LED_B_PIN,1);
                    }
                    else if (i == 1) {
                        led_control(LED_G_PIN,1);
                    }
                    else if (i == 2) {
                        rgb_led_control(1, 1, 1);
                    }

                    break;
                case KEY_EVENT_DOUBLE_CLICK:
                    rt_kprintf("Double Click\n");
                    if (i == 0 ) {
                            led_control(LED_B_PIN,0);
                        }
                        else if (i == 1) {
                            led_control(LED_G_PIN,0);
                        }
                        else if (i == 2) {
                            rgb_led_control(1, 1, 0);
                        }
                    break;
                case KEY_EVENT_LONG_PRESS:
                    rt_kprintf("Long Press\n");
                    break;
                default:
                    break;
                }
            }
        }
    }
}


/* 创建按键处理线程 */
int bsp_thread_init(void)
{


    /* 创建LED线程 */
    led_thread = rt_thread_create(
                            "led_blink",
                            led_thread_entry,
                            RT_NULL,
                            512,
                            RT_THREAD_PRIORITY_MAX / 2,
                            20);
    if (led_thread != RT_NULL) {
        rt_thread_startup(led_thread);
        rt_kprintf("led thread startup\n");
        return RT_EOK;
    }

    /* 创建LED线程 */
    key_thread = rt_thread_create(
                            "key_blink",
                            key_thread_entry,
                            RT_NULL,
                            1024,
                            RT_THREAD_PRIORITY_MAX / 2-1,
                            20);
    if (key_thread != RT_NULL) {
        rt_thread_startup(key_thread);
        rt_kprintf("key thread startup\n");
        return RT_EOK;
    }

    return -RT_ERROR;
}

/* 初始化按键线程 */
INIT_APP_EXPORT(bsp_thread_init);
